<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="仓库名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(pageIndex = 1)">查询</el-button>
        <el-button v-if="isAuth('basedata:warehouse:save')" type="primary" @click="addHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <table-tree-column
        prop="name"
        header-align="center"
        treeKey="id"
        width="150"
        label="仓库名称">
      </table-tree-column>
      <el-table-column
        prop="spelName"
        header-align="center"
        align="center"
        label="仓库拼码">
      </el-table-column>
      <el-table-column
        prop="parentName"
        header-align="center"
        align="center"
        label="上級仓库">
      </el-table-column>
      <el-table-column
        prop="resName"
        header-align="center"
        align="center"
        label="负责人姓名">
      </el-table-column>
      <el-table-column
        prop="tel"
        header-align="center"
        align="center"
        label="联系电话">
      </el-table-column>
      <el-table-column
        prop="remarks"
        header-align="center"
        align="center"
        label="备注信息">
      </el-table-column>
      <el-table-column
        prop="areaCode"
        header-align="center"
        align="center"
        label="区域编码">
      </el-table-column>
      <el-table-column
        prop="compId"
        header-align="center"
        align="center"
        label="公司ID">
      </el-table-column>
      <el-table-column
        prop="statusName"
        header-align="center"
        align="center"
        label="状态">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        label="更新者">
      </el-table-column>
      <el-table-column
        prop="updateDate"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addHandle(scope.row)">新增</el-button>
          <el-button type="text" size="small" @click="updateHandle(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row)">作废</el-button>
          <el-button type="text" size="small" @click="stopHandle(scope.row)" v-if="scope.row.status === '0' ">禁用</el-button>
          <el-button type="text" size="small" @click="openHandle(scope.row)" v-if="scope.row.status === '1' ">启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './warehouse-add-or-update'
  import TableTreeColumn from '@/components/common/table-tree-column'
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        dataForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      TableTreeColumn,
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(this.$API.BASE_INFO.WAREHOUSE_INFO.LIST),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({data}) => {
          this.dataList = treeDataTranslate(data, 'id')
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增
      addHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row, 'add')
        })
      },
      // 修改
      updateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      // 删除
      deleteHandle (row) {
        this.$confirm(`确定对${row.name}进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(this.$API.BASE_INFO.WAREHOUSE_INFO.INVALID),
            method: 'post',
            data: this.$http.adornData(row, false)
          }).then((data) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          })
        })
      },
      // 禁用
      stopHandle (row) {
        this.$http({
          url: this.$http.adornUrl(this.$API.BASE_INFO.WAREHOUSE_INFO.STOP),
          method: 'post',
          data: this.$http.adornData(row, false)
        }).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      },
      // 启用
      openHandle (row) {
        this.$http({
          url: this.$http.adornUrl(this.$API.BASE_INFO.WAREHOUSE_INFO.OPEN),
          method: 'post',
          data: this.$http.adornData(row, false)
        }).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      }
    }
  }
</script>
